Module (chicken numvector)
Homogeneous numeric vector datatypes. This module provides a superset of SRFI-4. The module srfi-4 is also available for compatibility reasons.
When loaded, the feature identifier srfi-4 is defined.
CHICKEN implementation specifics and extensions to SRFI-4
- Procedures for bytevector conversion, subvectors and vector I/O are provided.
- SRFI-17 setters for XXXvector-ref are defined.
- Constructors allow allocating the storage in non garbage collected memory.
- Vectors for 64 and 128-bit complex numbers are provided.
This module provides a set of datatypes for vectors whose elements are of the same numeric type (signed or unsigned exact integer or inexact real of a given precision). These datatypes support operations analogous to the Scheme vector type, but they are distinct datatypes. An external representation is specified which must be supported by the read and write procedures and by the program parser (i.e. programs can contain references to literal homogeneous vectors).
Datatypes
There are 8 datatypes of exact integer homogeneous vectors (which will be called integer vectors):
Datatype | Type of elements |
---|---|
s8vector | signed exact integer in the range -(2^7) to (2^7)-1 |
u8vector | unsigned exact integer in the range 0 to (2^8)-1 |
s16vector | signed exact integer in the range -(2^15) to (2^15)-1 |
u16vector | unsigned exact integer in the range 0 to (2^16)-1 |
s32vector | signed exact integer in the range -(2^31) to (2^31)-1 |
u32vector | unsigned exact integer in the range 0 to (2^32)-1 |
s64vector | signed exact integer in the range -(2^31) to (2^31)-1 |
u64vector | unsigned exact integer in the range 0 to (2^64)-1 |
s64vector | signed exact integer in the range -(2^63) to (2^63)-1 |
u64vector | unsigned exact integer in the range 0 to (2^64)-1 |
There are 2 datatypes of inexact real homogeneous vectors (which will be called float vectors):
Datatype | Type of elements |
---|---|
f32vector | inexact real |
f64vector | inexact real |
The only difference between the two float vector types is that f64vectors preserve at least as much precision as f32vectors.
And there are two datatypes of inexact complex homogeneous vectors (which will be called complex vectors):
Datatype | Type of elements |
---|---|
c64vector | inexact complex, typically 64 bits |
c128vector | inexact complex, typically 128 bits |
Each homogeneous vector datatype has an external representation which is supported by the read and write procedures and by the program parser. Each datatype also has a set of associated predefined procedures analogous to those available for Scheme's heterogeneous vectors.
External representation
[read] #u8[read] #u16
[read] #u32
[read] #s8
[read] #s16
[read] #s32
[read] #f32
[read] #f64
[read] #c64
[read] #c128
The external representation of instances of the datatype XXXvector is #XXX( ...elements... ).
For example,
#u8(0 #e1e2 #xff)}} ; a {{u8vector}} of length 3 containing 0, 100, 255 #f64(-1.5) ; a {{f64vector}} of length 1 containing -1.5.
This external representation is also available in program source code. For example,
(set! x '#u8(1 2 3))
will set x to the object #u8(1 2 3). Since CHICKEN 4.9.0, literal homogeneous vectors do not have to be quoted. Homogeneous vectors can appear in quasiquotations but must not contain unquote or unquote-splicing forms. I.e.,
`(,x #u8(1 2)) ; legal `#u8(1 ,x 2) ; illegal
Elements may also be characters or strings, in that case they are interpreted as a sequence of numerical character codes. For example,
'#u8(#\x7f "EL" #\F 2 1)
is equivalent to
'#u8(#\x7f #\x45 #\x4c #\x46 2 1)
Character literals inside numeric vectors expand into the UTF-8 sequence of the characters they represent, for strings the contained characters are interpreted in whatever encoding is used for the text file or stream in which the literal appears.
Note that #u8"..." can be used as an abbreviation for the special case #u8("...").
Predicates
[procedure] (u8vector? OBJ)[procedure] (s8vector? OBJ)
[procedure] (u16vector? OBJ)
[procedure] (s16vector? OBJ)
[procedure] (u32vector? OBJ)
[procedure] (s32vector? OBJ)
[procedure] (u64vector? OBJ)
[procedure] (s64vector? OBJ)
[procedure] (f32vector? OBJ)
[procedure] (f64vector? OBJ)
[procedure] (c64vector? OBJ)
[procedure] (c128vector? OBJ)
Return #t if obj is an object of the specified type or #f if not.
[procedure] (number-vector? OBJ)Return #t if obj is a number vector, #f if not. A "number vector" is any of the homogeneous number vector types defined by SRFI-4, ie it's one of u8vector, s8vector, u16vector, s16vector, u32vector, s32vector, u64vector, s64vector, f32vector, f64vector, c64vector or c128vector.
Constructors
[procedure] (make-u8vector N [U8VALUE NONGC FINALIZE])[procedure] (make-s8vector N [S8VALUE NONGC FINALIZE])
[procedure] (make-u16vector N [U16VALUE NONGC FINALIZE])
[procedure] (make-s16vector N [S16VALUE NONGC FINALIZE])
[procedure] (make-u32vector N [U32VALUE NONGC FINALIZE])
[procedure] (make-s32vector N [S32VALUE NONGC FINALIZE])
[procedure] (make-u64vector N [U64VALUE NONGC FINALIZE])
[procedure] (make-s64vector N [S64VALUE NONGC FINALIZE])
[procedure] (make-f32vector N [F32VALUE NONGC FINALIZE])
[procedure] (make-f64vector N [F64VALUE NONGC FINALIZE])
[procedure] (make-c64vector N [C64VALUE NONGC FINALIZE])
[procedure] (make-c128vector N [C128VALUE NONGC FINALIZE])
Return a newly-allocated SRFI-4 homogeneous number vector of length N.
If the optional fill VALUE is specified, it specifies the initial value for each slot in the vector. If not, the content of the vector is unspecified but individual elements of the vector are guaranteed to be in the range of values permitted for that type of vector.
The type of the fill value must be compatible with the elements of the vector datatype. It is an error if otherwise -- for example, if an inexact integer is passed to make-u8vector.
On CHICKEN, these procedures have been extended to allow allocating the storage in non-garbage collected memory, as follows:
The optional arguments NONGC and FINALIZE define whether the vector should be allocated in a memory area not subject to garbage collection and whether the associated storage should be automatically freed (using finalization) when there are no references from Scheme variables and data. NONGC defaults to #f (the vector will be located in normal garbage collected memory) and FINALIZE defaults to #t. Note that the FINALIZE argument is only used when NONGC is true.
[procedure] (u8vector U8VALUE ...)[procedure] (s8vector S8VALUE ...)
[procedure] (u16vector U16VALUE ...)
[procedure] (s16vector S16VALUE ...)
[procedure] (u32vector U32VALUE ...)
[procedure] (s32vector S32VALUE ...)
[procedure] (u64vector U64VALUE ...)
[procedure] (s64vector S64VALUE ...)
[procedure] (f32vector F32VALUE ...)
[procedure] (f64vector F64VALUE ...)
[procedure] (c64vector C64VALUE ...)
[procedure] (c128vector C128VALUE ...)
Return a newly-allocated SRFI-4 homogeneous number vector of the specified type, composed of the arguments.
Length
[procedure] (u8vector-length U8VECTOR)[procedure] (s8vector-length S8VECTOR)
[procedure] (u16vector-length U16VECTOR)
[procedure] (s16vector-length S16VECTOR)
[procedure] (u32vector-length U32VECTOR)
[procedure] (s32vector-length S32VECTOR)
[procedure] (u64vector-length U64VECTOR)
[procedure] (s64vector-length S64VECTOR)
[procedure] (f32vector-length F32VECTOR)
[procedure] (f64vector-length F64VECTOR)
[procedure] (c64vector-length C64VECTOR)
[procedure] (c128vector-length C128VECTOR)
Returns the length of the SRFI-4 homogeneous number VECTOR.
Getters
[procedure] (u8vector-ref U8VECTOR I)[procedure] (s8vector-ref S8VECTOR i)
[procedure] (u16vector-ref U16VECTOR I)
[procedure] (s16vector-ref S16VECTOR I)
[procedure] (u32vector-ref U32VECTOR I)
[procedure] (s32vector-ref S32VECTOR I)
[procedure] (u64vector-ref U64VECTOR I)
[procedure] (s64vector-ref S64VECTOR I)
[procedure] (f32vector-ref F32VECTOR I)
[procedure] (f64vector-ref F64VECTOR I)
[procedure] (c64vector-ref C64VECTOR I)
[procedure] (c128vector-ref C128VECTOR I)
Return the value of the ith element of the SRFI-4 homogeneous number vector, where I is a nonnegative exact integer less than the length of the vector.
Setters
[procedure] (u8vector-set! U8VECTOR I U8VALUE)[procedure] (s8vector-set! S8VECTOR I S8VALUE)
[procedure] (u16vector-set! U16VECTOR I U16VALUE)
[procedure] (s16vector-set! S16VECTOR I S16VALUE)
[procedure] (u32vector-set! U32VECTOR I U32VALUE)
[procedure] (s32vector-set! S32VECTOR I S32VALUE)
[procedure] (u64vector-set! U64VECTOR I U64VALUE)
[procedure] (s64vector-set! S64VECTOR I S64VALUE)
[procedure] (f32vector-set! F32VECTOR I F32VALUE)
[procedure] (f64vector-set! F64VECTOR I F64VALUE)
[procedure] (c64vector-set! C64VECTOR I C64VALUE)
[procedure] (c128vector-set! C128VECTOR I C128VALUE)
Set the ith element of the SRFI-4 homogeneous number VECTOR to VALUE. I is a nonnegative exact integer less than the length of the vector and VALUE must be the same type as the elements of the vector datatype.
Additionally, SRFI-17 setters are defined on all xxxvector-ref procedures. For example, to set the ith element of SRFI-4 u8vector to u8value:
(set! (u8vector-ref u8vector i) u8value)
Conversions
[procedure] (u8vector->list U8VECTOR)[procedure] (s8vector->list S8VECTOR)
[procedure] (u16vector->list U16VECTOR)
[procedure] (s16vector->list S16VECTOR)
[procedure] (u32vector->list U32VECTOR)
[procedure] (s32vector->list S32VECTOR)
[procedure] (u64vector->list U64VECTOR)
[procedure] (s64vector->list S64VECTOR)
[procedure] (f32vector->list F32VECTOR)
[procedure] (f64vector->list F64VECTOR)
[procedure] (c64vector->list C64VECTOR)
[procedure] (c128vector->list C128VECTOR)
Return a list consisting of the elements of SRFI-4 homogeneous number VECTOR.
[procedure] (list->u8vector U8LIST)[procedure] (list->s8vector S8LIST)
[procedure] (list->u16vector U16LIST)
[procedure] (list->s16vector S16LIST)
[procedure] (list->u32vector U32LIST)
[procedure] (list->s32vector S32LIST)
[procedure] (list->u64vector U64LIST)
[procedure] (list->s64vector S64LIST)
[procedure] (list->f32vector F32LIST)
[procedure] (list->f64vector F64LIST)
[procedure] (list->c64vector C64LIST)
[procedure] (list->c128vector C128LIST)
Return a newly-allocated SRFI-4 homogeneous number VECTOR consisting of the elements of LIST. Each element of LIST must be compatible with the datatype of VECTOR.
Blob conversions
As a number vector is basically just a bytevector wrapped into a record type, there are several procedures which can convert between bytevectors and number vectors.
Note that built-in bytevectors are identical to u8vectors.
[procedure] (s8vector->bytevector S8VECTOR)[procedure] (u16vector->bytevector U16VECTOR)
[procedure] (s16vector->bytevector S16VECTOR)
[procedure] (u32vector->bytevector U32VECTOR)
[procedure] (s32vector->bytevector S32VECTOR)
[procedure] (u64vector->bytevector U64VECTOR)
[procedure] (s64vector->bytevector S64VECTOR)
[procedure] (f32vector->bytevector F32VECTOR)
[procedure] (f64vector->bytevector F64VECTOR)
[procedure] (c64vector->bytevector C64VECTOR)
[procedure] (c128vector->bytevector C128VECTOR)
[procedure] (u8vector->bytevector/shared U8VECTOR)
[procedure] (s8vector->bytevector/shared S8VECTOR)
[procedure] (u16vector->bytevector/shared U16VECTOR)
[procedure] (s16vector->bytevector/shared S16VECTOR)
[procedure] (u32vector->bytevector/shared U32VECTOR)
[procedure] (s32vector->bytevector/shared S32VECTOR)
[procedure] (u64vector->bytevector/shared U64VECTOR)
[procedure] (s64vector->bytevector/shared S64VECTOR)
[procedure] (f32vector->bytevector/shared F32VECTOR)
[procedure] (f64vector->bytevector/shared F64VECTOR)
[procedure] (c64vector->bytevector/shared C64VECTOR)
[procedure] (c128vector->bytevector/shared C128VECTOR)
Each of these procedures return the contents of the given vector as a 'packed' bytevector. The byte order in that vector is platform-dependent (for example little-endian on an Intel processor). The /shared variants return a bytevector that shares memory with the contents of the vector, the others will copy the contents of the vector's internal bytevector object.
[procedure] (bytevector->s8vector BYTEVECTOR)[procedure] (bytevector->u16vector BYTEVECTOR)
[procedure] (bytevector->s16vector BYTEVECTOR)
[procedure] (bytevector->u32vector BYTEVECTOR)
[procedure] (bytevector->s32vector BYTEVECTOR)
[procedure] (bytevector->u64vector BYTEVECTOR)
[procedure] (bytevector->s64vector BYTEVECTOR)
[procedure] (bytevector->f32vector BYTEVECTOR)
[procedure] (bytevector->f64vector BYTEVECTOR)
[procedure] (bytevector->c64vector BYTEVECTOR)
[procedure] (bytevector->c128vector BYTEVECTOR)
[procedure] (bytevector->s8vector/shared BYTEVECTOR)
[procedure] (bytevector->u16vector/shared BYTEVECTOR)
[procedure] (bytevector->s16vector/shared BYTEVECTOR)
[procedure] (bytevector->u32vector/shared BYTEVECTOR)
[procedure] (bytevector->s32vector/shared BYTEVECTOR)
[procedure] (bytevector->u64vector/shared BYTEVECTOR)
[procedure] (bytevector->s64vector/shared BYTEVECTOR)
[procedure] (bytevector->f32vector/shared BYTEVECTOR)
[procedure] (bytevector->f64vector/shared BYTEVECTOR)
[procedure] (bytevector->c64vector/shared BYTEVECTOR)
[procedure] (bytevector->c128vector/shared BYTEVECTOR)
Each of these procedures return a vector where the argument BYTEVECTOR is taken as a 'packed' representation of the contents of the vector. The /shared variants return a vector that shares memory with the contents of the bytevector, the others will copy the bytevector.
Subvectors
[procedure] (subu8vector U8VECTOR FROM TO)[procedure] (subu16vector U16VECTOR FROM TO)
[procedure] (subu32vector U32VECTOR FROM TO)
[procedure] (subu64vector U32VECTOR FROM TO)
[procedure] (subs8vector S8VECTOR FROM TO)
[procedure] (subs16vector S16VECTOR FROM TO)
[procedure] (subs32vector S32VECTOR FROM TO)
[procedure] (subs64vector S32VECTOR FROM TO)
[procedure] (subf32vector F32VECTOR FROM TO)
[procedure] (subf64vector F64VECTOR FROM TO)
[procedure] (subc64vector C64VECTOR FROM TO)
[procedure] (subc128vector C128VECTOR FROM TO)
Creates a fresh number vector of the same type as the argument vector with the elements at the positions FROM up to but not including TO.
Release number vectors allocated in static memory
[procedure] (release-number-vector NVECTOR)Release the storage of a SRFI-4 vector that was allocated in non-garbage collected memory (for example using the NONGC argument for one of the make-XXXvector constructor procedures). The effect of calling this procedure with a number vector allocated in normal garbage collected memory is undefined.
Previous: Module (chicken type)